<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
import DialogEditor from '@/components/DialogEditor'
import dialogMixins from '@/utils/commonDialogMixins'
import {mRequest} from '@/utils/request'

export default {
  mixins: [dialogMixins],
  components: {DialogEditor},
  data() {
    return {
      dataFormConfig: {
        id: {
          hidden: true
        },
        dayTime: {
          label: '日期',
          type: 'date',
          dateFormat: 'yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒',
          valueFormat: 'yyyy-MM-dd HH:mm:ss',
          placeholder: '请选择日期'
        },
        redBusinessType: {
          label: '红冲业务',
          type: 'radio',
          radioBorder: true,
          radioList: [{text: '产品出库单', label: '0'}, {text: '纸箱入库单', label: '1'},{text: '过磅记录单', label: '2'}],
          default: '0'
        },
        reversalType: {
          label: '红冲类型',
          type: 'radio',
          radioBorder: true,
          radioList: [{text: '冲减', label: '1'},{text: '冲增', label: '0'}],
          default: '1'
        },
        productLeaveNo: {
          label: '产品出库单编号',
        },
        productNo: {
          label: '产品编号',
        },
        productId: {
          hidden: true,
        },
        clientName: {
          label: '客户名称',
        },
        clientId: {
          hidden: true,
        },
        productLeaveId: {
          hidden: true,
          label: '产品出库单',
          placeholder: '请选择产品出库单',
        },
        reversalTransactionProductId: {
          hidden: true
        },
        boxAddId: {
          reply: () => this.dataForm.redBusinessType=='1',
          label: '纸箱入库单',
          type: 'list',
          placeholder: '请选择纸箱入库单',
          list: [],
        },
        weighDetailInfoId: {
          reply: () => this.dataForm.redBusinessType=='2',
          label: '过磅记录单',
          type: 'list',
          placeholder: '请选择过磅记录单',
          list: [],
        },
        unitPrice: {
          label: '单价',
          type: 'number',
          max: 1000,
          min: 0,
          disabled: true,
          default: 0
        },
        boxZhiNumber: {
          label: '箱只',
          type: 'number',
          max: 10000000,
          min: 0,
          default: 0,
          selectCallback: this.numberChanged
        },
        boxNumber: {
          label: '箱数',
          type: 'number',
          max: 10000000,
          min: 0,
          default: 0,
          selectCallback: this.numberChanged
        },
        scrapNumber: {
          label: '零头',
          type: 'number',
          max: 10000000,
          min: 0,
          default: 0,
          selectCallback: this.numberChanged
        },
        reversalNumber: {
          label: '合计数量',
          type: 'number',
          max: 10000000,
          min: 0,
          default: 0,
          disabled: true,
        },
        freightAmountPrice:{
          label: '运费',
          type: 'number',
          max: 10000000,
          min: 0,
          default: 0,
          selectCallback: this.numberChanged
        },
        totalPrice: {
          label: '总金额',
          type: 'number',
          max: 100000000,
          min: 0,
          default: 0
        },
        reversalImageUrl: {
          label: '截图留存',
          type: 'uploadScreenshot',
          imageType: 'uploadProductImage'
        },

        remark: {
          label: '备注',
          inputType: 'textarea',
          placeholder: '请输入备注'
        },
        status: {
          label: '状态',
          type: 'switch',
          activeValue: 1,
          inactiveValue: 0,
          default: 1
        }
      }
    }
  },
  methods: {
    addNewIngredient() {
      const cb = (newValue) => {
        this.initIngredientInfoList()
        this.dataForm.ingredientInfoId = newValue.id
      }
      this.$parent.openIngredientInfoDialog(cb)
    },
    ingredientIdSelectCallback() {
      //getSupplierListByIngredientId
      this.dataForm.supplierId = ''

      mRequest('supplierApi/getSupplierListByIngredientId', {id: this.dataForm.ingredientInfoId}, ({data}) => {
        this.dataFormConfig['supplierId'].list = this.R.propOr([], 'list', data)
      })

    },
    numberChanged() {
      const boxNumber = this.R.propOr(0, 'boxNumber', this.dataForm)
      const boxZhiNumber = this.R.propOr(0, 'boxZhiNumber', this.dataForm)
      const scrapNumber = this.R.propOr(0, 'scrapNumber', this.dataForm)

      const unitPrice = this.R.propOr(0, 'unitPrice', this.dataForm)
      const reversalNumber = boxNumber * boxZhiNumber + scrapNumber
      const freightAmountPrice = this.R.propOr(0, 'freightAmountPrice', this.dataForm)

      this.dataForm.reversalNumber = reversalNumber
      this.dataForm.totalPrice = unitPrice * reversalNumber + freightAmountPrice
    },
    init(id,property, cb) {
      this.dataForm.id = id || 0
      this.$refs.dialog.initDialog({
        dataForm: this.dataForm,
        config: this.dataFormConfig,
        dataRule: this.getNeedDataRule( 'dayTime'),
        initApi: 'financeApi/getFinanceReversalRecordsInfo',
        saveApi: 'financeApi/saveFinanceReversalRecords',
        updateApi: 'financeApi/updateFinanceReversalRecords',
        objectName: 'financeReversalRecords',
        submitCallback: cb
      })

        this.$nextTick(() => {
          if (!this.R.isNil(property)) {
            Object.keys(property).forEach((key) => {
              this.dataForm[key] = property[key]
            })
          }
        })
    }
  }
}
</script>
